package yb.ecp.fast.user.web;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.Ref;
import yb.ecp.fast.user.infra.AuthCode;
import yb.ecp.fast.user.infra.BasicController;
import yb.ecp.fast.user.infra.ErrorCode;
import yb.ecp.fast.user.service.TemplateService;
import yb.ecp.fast.user.service.VO.TemplateConfigVO;
import yb.ecp.fast.user.service.VO.TemplateVO;
import yb.ecp.fast.user.service.VO.TmpAuthsVO;
import yb.ecp.fast.user.service.VO.TmpMenusVO;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiChangjiang on 2017/12/26 0026.
 */

@RestController
@RequestMapping("/template")
public class TemplateController extends BasicController
{
    @Autowired
    TemplateService templateService;

    @Autowired
    private FastGenClient fastGenClient;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有权限模板")
//    @FastMappingInfo(code = AuthCode.Template.LIST)
    public ActionResult listTmp()
    {
        return actionResult(templateService.queryList());
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增一个模板")
    @FastMappingInfo(code = AuthCode.Template.INSERT)
    public ActionResult insert(@RequestBody TemplateVO templateVO)
    {
        //获取ID
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode())
        {
            return textResult;
        }
        templateVO.setId(textResult.getValue());
        return actionResult(templateService.insert(templateVO));
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除模板")
    @FastMappingInfo(code = AuthCode.Template.BATCH_REMOVE)
    public ActionResult remove(@RequestBody List<String> ids)
    {
        Ref<List> tmpRef = new Ref<>(new ArrayList());
        return actionResult(templateService.removeByIds(ids,tmpRef),tmpRef.get());
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改权限模板信息")
    @FastMappingInfo(code = AuthCode.Template.UPDATE)
    public ActionResult update(@RequestBody TemplateVO templateVO)
    {
        return actionResult(templateService.update(templateVO));
    }

    @RequestMapping(value = "/itemById", method = RequestMethod.GET)
    @ApiOperation(value = "通过ID查看模板信息")
    public ActionResult itemById(@RequestParam("id") String id)
    {
        return actionResult(templateService.selectById(id));
    }


    @RequestMapping(value = "/configTmpAuth", method = RequestMethod.POST)
    @ApiOperation(value = "配置模板权限")
    @FastMappingInfo(code = AuthCode.Template.CONFIG_TMP_AUTH)
    public ActionResult configTmpAuth(@RequestBody TmpAuthsVO tmpAuthsVO)
    {
        ErrorCode ecc = templateService.configTmpAuth(tmpAuthsVO);
        return actionResult(ecc);
    }

    @RequestMapping(value = "/configTemplate", method = RequestMethod.POST)
    @ApiOperation(value = "配置模板")
    @FastMappingInfo(code = AuthCode.Template.CONFIG_TEMPLATE)
    public ActionResult configTemplate(@RequestBody TemplateConfigVO templateConfigVO,@RequestHeader("x-from-site") String site)
    {
        ErrorCode ecc = templateService.configTemplate(templateConfigVO);
        return actionResult(ecc);
    }

    @RequestMapping(value = "/configTmpMenu", method = RequestMethod.POST)
    @ApiOperation(value = "配置模板MENU")
    @FastMappingInfo(code = AuthCode.Template.CONFIG_TMP_MENU)
    public ActionResult configTmpMenu(@RequestBody TmpMenusVO tmpMenusVO)
    {
        ErrorCode ecc = templateService.configTmpMenu(tmpMenusVO);
        return actionResult(ecc);
    }

    @RequestMapping(value = "/queryTemplateConfig", method = RequestMethod.GET)
    @ApiOperation(value = "查询模板的权限与MENU信息")
    public ActionResult queryTemplateConfig(@RequestParam("tempId") String tempId)
    {
        return actionResult(templateService.queryTemplateConfigById(tempId));
    }

    @RequestMapping(value = "/queryMenuByTemplate", method = RequestMethod.GET)
    @ApiOperation(value = "查询模板下的MENU集合")
    public ActionResult queryMenuByTemplate(@RequestParam("tempId") String tempId)
    {
        return actionResult(templateService.queryMenuByTemplate(tempId));
    }

    @RequestMapping(value = "/queryAuthByTemplate", method = RequestMethod.GET)
    @ApiOperation(value = "查询模板下的权限CODE集合")
    @FastMappingInfo(needLogin = true)
    public ActionResult queryAuthByTemplate(@RequestParam("tempId") String tempId)
    {
        return actionResult(templateService.queryAuthByTemplate(tempId));
    }

    @RequestMapping(value = "/removeAuthByTemplate", method = RequestMethod.POST)
    @ApiOperation(value = "删除模板下的所有权限")
    public ActionResult removeAuthByTemplate(@RequestParam("tempId") String tempId)
    {
        return actionResult(templateService.removeAuthByTemplate(tempId));
    }

    @RequestMapping(value = "/removeMenuByTemplate", method = RequestMethod.POST)
    @ApiOperation(value = "删除模板下的所有MENU")
    @FastMappingInfo(code = AuthCode.Template.REMOVE_TEMPLATE_MENUS)
    public ActionResult removeMenuByTemplate(@RequestParam("tempId") String tempId)
    {
        return actionResult(templateService.removeMenuByTemplate(tempId));
    }

    @RequestMapping(value = "/removeMenus", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除模板下的MENU")
    @FastMappingInfo(code = AuthCode.Template.BATCH_REMOVE_MENUS)
    public ActionResult removeMenus(@RequestBody TmpMenusVO tmpMenusVO)
    {
        return actionResult(templateService.removeMenus(tmpMenusVO));
    }

    @RequestMapping(value = "/removeAuths", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除模板下的权限")
    @FastMappingInfo(code = AuthCode.Template.BATCH_REMOVE_AUTHS)
    public ActionResult removeAuths(@RequestBody TmpAuthsVO tmpAuthsVO)
    {
        return actionResult(templateService.removeAuths(tmpAuthsVO));
    }
}
